/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bean.dataManipulation.Report;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.bo.ReportBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.ipti.br.tag.bo.ActorBO;

/**
 *
 * @author raabeNoa
 */
@ManagedBean(name = "reportsBean")
@ViewScoped
public class ReportsBean extends AppBaseBean implements Serializable {

    @Inject private SessionBean sessionBean;
    @Inject private UnityBO unityBO;
    @Inject private HumanBO humanBO;
    @Inject private ReportBO reportBO;
    private int year = getActualYear();
    private Unity unity;
    private Human human;
    private Actor actor;
    private Report selectedReport;
    

    public void report() {
        switch (selectedReport) {
            case STUDENTS_PER_CLASS:
                try {
                    reportBO.reportStudentsPerUnity(unity, sessionBean.getLoggedActor().getUnity());
                } catch (Exception ex) {
                    Logger.getLogger(ReportsBean.class.getName()).log(Level.SEVERE, null, ex);
                    this.addErrorMessage(null, "Deu um erro :/", "Acho melhor informar ao administrador");
                }
                break;
//            case SCHOOL_PERFORMANCE:
                //reportSchoolPerformance();
//                break;
            case DECLARATION:
                try {
                    reportBO.declaration();
                } catch (Exception ex) {
                    Logger.getLogger(ReportsBean.class.getName()).log(Level.SEVERE, null, ex);
                    this.addErrorMessage(null, "Deu um erro :/", "Acho melhor informar ao administrador");
                }
                break;
        }
    }

    public List<Unity> getUnityList() {
        return unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(), year);
    }

    public List<Unity> getUnityStudentList() {
        List<Unity> unities = new ArrayList<Unity>();

        if (human == null) {
            return unities;
        }

        List<Actor> actors = human.getActors();

        //não faz sentido
        for (Actor actor : actors) {
            if ((actor.getDeactivated() == null || actor.getDeactivated().after(Calendar.getInstance()))) {
                if ((actor.getUnity().getDeactivated() == null || actor.getUnity().getDeactivated().after(Calendar.getInstance()))) {
                    unities.add(actor.getUnity());
                }
            }
        }

        return unities; 
    }
    
    public List<Actor> getStudentList() {
        if (unity == null) {
            return new ArrayList<Actor>();
        }
        return unity.getActors();
    }

    public List<Human> searchHuman(String selectedStudent) {
        return humanBO.getHumanDAO().searchLimitedHumanPerName(selectedStudent, 10, null);
    }

    public Report[] getReportList() {
        return Report.values();
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public Report getSelectedReport() {
        return selectedReport;
    }

    public void setSelectedReport(Report selectedReport) {
        this.selectedReport = selectedReport;
    }

    public Human getHuman() {
        return human;
    }

    public void setHuman(Human human) {
        this.human = human;
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }
    
    
}
